﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class CatalogSupplierRatingTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase(1)]
        public void Can_CRUD(byte value)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var supplier = Processor.Execute(new CreateCatalogSupplier
            {
                Flags = CatalogSupplierFlags.RatingAllowed
            }).Result;

            #endregion

            var rating = Processor.Execute(new CreateCatalogSupplierRating
            {
                AccountID = account.EntityID,
                HolderID = supplier.EntityID,
                Value = value
            }).Result;

            Assert.NotNull(rating);
            rating.Value.AreEqual(value);
            rating.AreEqual(Processor.Execute(new GetCatalogSupplierRating
            {
                EntityID = rating.EntityID
            }).Result);

            value = (byte)(value + 1);
            var upd_rating = Processor.Execute(new UpdateCatalogSupplierRating
            {
                EntityID = rating.EntityID,
                Value = value
            }).Result;

            Assert.NotNull(upd_rating);
            upd_rating.AreEqual(rating);
            upd_rating.Value.AreEqual(value);

            Processor.Execute(new DeleteCatalogSupplier
            {
                EntityID = rating.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Not_Allowed()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateCatalogSupplierRating
            {
                AccountID = 0,
                HolderID = Processor.Execute(new CreateCatalogSupplier
                {
                    Flags = CatalogSupplierFlags.None
                }).Result.EntityID,
                Value = 0
            }));
        }
    }
}
